"""
剑指 Offer II 021. 删除链表的倒数第 n 个结点
给定一个链表，删除链表的倒数第 n 个结点，并且返回链表的头结点。

示例 1：
输入：head = [1,2,3,4,5], n = 2
输出：[1,2,3,5]

示例 2：
输入：head = [1], n = 1
输出：[]

示例 3：
输入：head = [1,2], n = 1
输出：[1]
"""
from mode import *


# 返回链表节点
class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        pre, cur = None, head
        listLen = 0
        curLen = 0
        while cur:
            listLen += 1
            cur = cur.next
        if n == listLen:
            return head.next
        cur = head
        while curLen + n < listLen:
            curLen += 1
            pre = cur
            cur = cur.next
            if curLen + n == listLen:
                pre.next = cur.next
                return head


if __name__ == "__main__":
    """
    输入：head = [1,2,3]
    输出：[1,3]
    """
    a1 = ListNode(1)
    a2 = ListNode(2)
    a3 = ListNode(3)
    a1.next = a2
    # a2.next = a3

    A = Solution()
    A.removeNthFromEnd(a1, 2)
    PrintListNode(a1)
    res = set()
    nums = [1,2,3]
    res.add(nums)
    res.add(nums)
    print(res)